package com.manager.devicemanager.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.manager.devicemanager.common.DataSourceNameEnum;
import com.manager.devicemanager.common.Result;
import com.manager.devicemanager.entity.*;
import com.manager.devicemanager.service.IComponent1Service;
import com.manager.devicemanager.service.IDevicestatusService;
import com.manager.devicemanager.service.IComponentRunStatus1Service;
import com.manager.devicemanager.util.StringDealUtils;
import com.manager.devicemanager.vo.ComponentStatusRequestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wpf
 * @since 2025-05-29
 */
@RestController
@RequestMapping("/iccomponent1")
public class Component1Controller {
    @Autowired
    private IComponent1Service iComponent1Service;
    @Autowired
    private IComponentRunStatus1Service iStatus1Service;
    @Autowired
    private IDevicestatusService iDevicestatusService;

    @PostMapping("getall")
    public Result< Map<String,Long>> getAll( @RequestBody ComponentStatusRequestVO componentStatusRequestVO){
        Map<String,Long> result=new HashMap<>();
        for (int i=1; i <= 12; i++) {
            result.put(""+i,iComponent1Service.countdynamicData(DataSourceNameEnum.ICBOARDATA.getDataSourceName(),"component_"+componentStatusRequestVO.getTestid()+"_"+i));
        }
//        result.put("1",iComponent1Service.countdynamicData("component_1_1"));
        return Result.success(result);

    }

    @PostMapping("getdynamicDataLimit48")
    public Result<List<Component1>> getdynamicDataLimit48(@RequestBody ComponentStatusRequestVO componentStatusRequestVO){
        List<Component1> component1s = iComponent1Service.getdynamicDataLimit48(DataSourceNameEnum.ICBOARDATA.getDataSourceName()+componentStatusRequestVO.getDatasourceindex(),"component_" + componentStatusRequestVO.getTestid() + "_" + componentStatusRequestVO.getBoardid());
        component1s.sort(Comparator.comparing(Component1::getComponentid).reversed());
//        ComponentRunStatus1 lastData = iStatus1Service.getLastData(DataSourceNameEnum.ICBOARDCONFIG.getDataSourceName()+componentStatusRequestVO.getDatasourceindex());
//        JSONArray jsonArray = new JSONArray();
//        String[] statusArray = null;
//        if (lastData != null) {
//            String methodName = "getS"+componentStatusRequestVO.getBoardid();
//            String s1 =null;
//            try {
//                java.lang.reflect.Method method = ComponentRunStatus1.class.getMethod(methodName);
//                s1 = (String) method.invoke(lastData);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//            if (s1 != null) {
//                s1 = StringDealUtils.dealIComponentString(s1);
//                statusArray = s1.split(",");
//            }
//        }
//        for(int i = 1; i <= component1s.size(); i++) {
//            Component1 component1 = component1s.get(i - 1);
//            // 使用 FastJSON 直接将 Component1 对象转换为 JSONObject
//            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(component1));
//            jsonObject.put("status", statusArray!= null && statusArray.length >= component1.getComponentid()? statusArray[component1.getComponentid() - 1] : "");
//            jsonArray.add(jsonObject);
//        }


        return  Result.success(component1s);
    }
    @PostMapping("getOneComponentLine")
    public Result<List<Component1>> getOneComponentLine(@RequestBody ComponentStatusRequestVO componentStatusRequestVO){
        List<Component1> component1s = iComponent1Service.getOneComponentLine(DataSourceNameEnum.ICBOARDATA.getDataSourceName()
                +componentStatusRequestVO.getDatasourceindex(),"component_" + componentStatusRequestVO.getTestid() + "_" + componentStatusRequestVO.getBoardid(),componentStatusRequestVO);
        return Result.success(component1s);
    }

}
